package com.isnmp.common.snmp.impl;

import java.util.List;
import java.util.Vector;

import net.percederberg.mibble.MibValueSymbol;

import org.snmp4j.Snmp;
import org.snmp4j.Target;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.Variable;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.util.DefaultPDUFactory;
import org.snmp4j.util.TableEvent;
import org.snmp4j.util.TableUtils;

import com.isnmp.common.base.MyException;
import com.isnmp.common.snmp.OMMappingInfo;

public class GetAllAction {

    /**
     * 返回MibEntry集合列表
     * @param snmp
     * @param target
     * @param paramClass  
     * @return 
     * @throws MyException
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public static List getAllTableData(Snmp snmp, Target target, Class paramClass) throws MyException {
        String str1;
        try {
            Vector localVector1 = new Vector();
            str1 = SNMPUtils.getMibOIDofClass(paramClass);
            MibValueSymbol localMibValueSymbol = SNMPAPIImpl.getInstance().getMibSymbolByOid(str1);
            //一个mib下有多少个子mib
            Vector localVector2 = SNMPUtils.getFieldsInMibNode(paramClass, localMibValueSymbol);
            OID[] arrayOfOID = new OID[localVector2.size()];//得到对应的所有oid
            for (int i = 0; i < localVector2.size(); i++) {
                Object localObject1 = (MibValueSymbol) localVector2.get(i);
                ((MibValueSymbol) localObject1).getValue().toString();
                Object localObject2 = ((MibValueSymbol) localObject1).getValue().toString();
                arrayOfOID[i] = new OID((String) localObject2);
            }
            Vector localVector3 = new Vector();
            Object localObject1 = new TableUtils(snmp, new DefaultPDUFactory());
            //通过snmp4j得到所有的值
            Object localObject2 = ((TableUtils) localObject1).getTable(target, arrayOfOID, null, null);
            Object localObject3;
            for (int j = 0; j < ((List) localObject2).size(); j++) {
                localObject3 = (TableEvent) ((List) localObject2).get(j);
                if (((TableEvent) localObject3).isError())
                    throw new MyException(Messages.getString("GetAllAction.0") + ((TableEvent) localObject3).getErrorMessage());
                if (((TableEvent) localObject3).getException() != null)
                    throw new MyException(Messages.getString("GetAllAction.1") + ((TableEvent) localObject3).getErrorMessage() + " " + ((TableEvent) localObject3).getException().getMessage());
                if (((TableEvent) localObject3).getStatus() != 0)
                    throw new MyException(Messages.getString("GetAllAction.3") + ((TableEvent) localObject3).getErrorMessage());
                if ((((TableEvent) localObject3).getColumns() != null) && (((TableEvent) localObject3).getColumns().length >= 1))
                    localVector3.add(localObject3);
            }
            for (int j = 0; j < localVector3.size(); j++) {
                localObject3 = paramClass.newInstance();
                localVector1.add(localObject3);
                TableEvent localTableEvent = (TableEvent) localVector3.get(j);
                if ((localObject3 instanceof OMMappingInfo))
                    ((OMMappingInfo) localObject3).setTableIndexOID(localTableEvent.getIndex().toString());
                //这里面封装的就是值了
                VariableBinding[] arrayOfVariableBinding = localTableEvent.getColumns();
                for (int k = 0; k < arrayOfVariableBinding.length; k++)
                    try {
                        VariableBinding localVariableBinding = arrayOfVariableBinding[k];
                        if (localVariableBinding != null) {
                            String str2 = localVariableBinding.getOid().toString();
                            //得到每一个oid  并转成名字
                            String str3 = localMibValueSymbol.getMib().getSymbolByOid(str2).getName();
                            Variable localVariable = localVariableBinding.getVariable();
                            //封装成一个对象
                            SNMPUtils.setFieldValue(localObject3, str3, localVariable);
                        }
                    } catch (Exception localException2) {
                        localException2.printStackTrace();
                    }
            }
            return localVector1;
        } catch (Exception localException1) {
            str1 = Messages.getString("GetAllAction.4") + paramClass.getName() + Messages.getString("GetAllAction.5") + target.getAddress() + Messages.getString("GetAllAction.6") + localException1.getMessage();
        }
        throw new MyException(str1);
    }

    public static void main(String[] paramArrayOfString) {
        new GetAllAction();
    }
}